﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wicresoft.SMS.Data
{
    public class TemplateDataAccess : ITemplateService
    {
        private IConnectStringProvider csp;

        public TemplateDataAccess(IConnectStringProvider _csp)
        {
            csp = _csp;
        }

        public int GetPageCount(int UserId, int max, string key)
        {
            using (var dbacc = new Entities(csp.ConnectionString))
            {
                var data = from a in dbacc.SMSTemplate
                           where a.UserID == UserId
                           where a.Content.Contains(key)
                           select a;
                int count = (data.Count() + (max - 1)) / max;

                return count;
            }
        }

        public List<TemlateInfo> GetTemplatesByUserID(int UserId, int index, int max, string key)
        {
            using (var dbacc = new Entities(csp.ConnectionString))
            {
                var data = (from a in dbacc.SMSTemplate
                            where a.UserID == UserId
                            where a.Content.Contains(key)
                            orderby a.ID
                            select a).Skip((index - 1) * max).Take(max);

                List<TemlateInfo> list = new List<TemlateInfo>();
                foreach (var item in data)
                {
                    TemlateInfo ti = new TemlateInfo();
                    ti.ID = item.ID;
                    ti.Content = item.Content;
                    list.Add(ti);
                }

                return list;
            }
        }

        public bool DeleteTemplateByID(int ID)
        {
            using (var dbacc = new Entities(csp.ConnectionString))
            {
                var deleteitem = dbacc.SMSTemplate.FirstOrDefault(a => a.ID == ID);
                if (deleteitem != null)
                {
                    dbacc.SMSTemplate.DeleteObject(deleteitem);
                    dbacc.SaveChanges();
                    return true;
                }
                else
                    return false;

            }
        }

        public bool DeleteTemplates(int[] IDs)
        {
            using (var dbacc = new Entities(csp.ConnectionString))
            {
                foreach (var ID in IDs)
                {
                    var deleteitem = dbacc.SMSTemplate.FirstOrDefault(a => a.ID == ID);
                    dbacc.SMSTemplate.DeleteObject(deleteitem);
                }
                dbacc.SaveChanges();
                return true;
            }
        }

        public bool UpdateTemplate(int ID, string Content)
        {
            using (var dbacc = new Entities(csp.ConnectionString))
            {
                var updateitem = dbacc.SMSTemplate.FirstOrDefault(a => a.ID == ID);
                updateitem.Content = Content;
                dbacc.SaveChanges();
                return true;
            }
        }

        public bool CreateNewTemplate(int UserID, string Content)
        {
            using (var dbacc = new Entities(csp.ConnectionString))
            {
                SMSTemplate newitem = new SMSTemplate();
                newitem.UserID = UserID;
                newitem.Content = Content;
                dbacc.AddToSMSTemplate(newitem);
                dbacc.SaveChanges();
                return true;
            }
        }


    }
}
